package com.bicon.botu.mall.order.rpc.service;

import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.mall.order.common.OrderConstans;
import com.bicon.botu.mall.order.entity.OrderGoods;
import com.bicon.botu.mall.order.service.IOrderChildService;
import com.bicon.botu.mall.order.service.IOrderGoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Service
public class OrderGoodsRpcService {

    @Autowired
    private IOrderGoodsService iOrderGoodsService;

    @Autowired
    private IOrderChildService iOrderChildService;

    /**
     * 修改子订单评价状态、然后再判断是否修改主订单状态
     * @param orderGoodsId 分组订单ID
     * @param orderChildIds 子订单ID集合
     * @return
     */
    @RequestMapping(value = "updateEvaluateState",method = RequestMethod.POST)
    public BaseResult updateEvaluateState(@RequestParam(required = false) String orderGoodsId,
                                          @RequestParam(required = false) List<String> orderChildIds){
        if(StringUtils.isNotBlank(orderGoodsId) && orderChildIds != null && orderChildIds.size()>0){
            //修改子订单状态为已评价
            if(iOrderChildService.updateEvaluateState(orderChildIds)){
                //根据主订单Id查询所有已评价子订单总数和子订单总数
                Map<String,Long> integerMap=iOrderChildService.getEvaluateState(orderGoodsId);
                Long childEvaluateCount=integerMap.get("childEvaluateCount");
                Long orderChildCount=integerMap.get("orderChildCount");
                //判断子订单总数是否与已评价总数相等，是则修改分组订单已评价状态
                if(childEvaluateCount.equals(orderChildCount)){
                    OrderGoods orderGoods=new OrderGoods();
                    orderGoods.setId(orderGoodsId);
                    orderGoods.setEvaluateState(OrderConstans.UN_EVALUATE_STATE);
                    iOrderGoodsService.updateById(orderGoods);
                }
                return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200);
            }else{
                return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
            }
        }else{
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
        }
    }

}
